<template>
    <el-container>
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="cms-btn-group">
                        <el-button type="primary" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                        <el-button type="primary" icon="fa fa-check-square" @click="checkAll()" size="small"> 全选</el-button>
                    </div>
                </div>
                <el-form v-loading="loading"
                         element-loading-text="努力加载中..."
                         element-loading-spinner="el-icon-loading"
                         :model="form" ref="form" class="cms-access">
                    <el-input type="hidden" v-model="form.id"></el-input>
                    <div v-for="(item,index) in listData" :key="index">
                        <p><i class="fa fa-caret-right"></i> {{item.name}}</p>
                        <el-form-item v-for="(child,i) in item.children" :key="i">
                            <label @click="checkChild(child.children)" title="点我全选该组" alt="点我全选该组" class="cms-access-label">{{child.name}}：</label>
                            <el-checkbox-group v-model="form.rules">
                                <el-checkbox v-for="(c,i) in child.children" :label="c.id" :key="i">{{c.name}}</el-checkbox>
                            </el-checkbox-group>
                        </el-form-item>
                    </div>
                    <el-form-item>
                        <el-button type="primary" :loading="buttonLoading" :disabled="formDisabled" @click="submitForm('ruleForm')">{{buttonText}}</el-button>
                    </el-form-item>
                </el-form>
            </section>
        </el-main>
    </el-container>
</template>

<script>
    import {mapGetters} from 'vuex';
    import {NodeList,RoleAccess,RoleAccessUpdate} from '@/utils/request';
    export default {
        data() {
            return {
                listData: [],
                buttonLoading : false,
                buttonText: '提 交',
                formDisabled : false,
                all: false,
                form: {
                    id: 0,
                    rules: [],
                },
            }
        },
        activated(){
            this.$store.dispatch('setActive', '/operate/role');
            this.list();
        },
        computed: {
            ...mapGetters(['loading']),
        },
        methods: {
            //列表
            list(){
                this.$store.dispatch('setLoading', true);
                NodeList().then((res)=>{
                    if (res.code) {
                        this.$message.error(res.msg);
                    } else {
                        this.listData = res.data;
                    }
                    this.$store.dispatch('setLoading', false);
                })
            },
            getAccess(rid){
                RoleAccess({params:{id:rid}}).then((response)=>{
                    if(response.code){
                        this.$message.error(response.msg);
                    }else{
                        this.form.rules = response.data.rules;
                    }
                })
            },
            checkChild(arr){
                var inc = 0;
                arr.forEach((i)=>{
                    if(this.form.rules.includes(i.id)) inc ++;
                })
                if(inc){
                    //包含部分或者包含所有
                    if(inc == arr.length){
                        //包含所有，则要删除该组数据
                        arr.forEach((i)=>{
                            this.form.rules.forEach((item,e)=>{
                                if(i.id == item) this.form.rules.splice(e,1);
                            })
                        })
                    }else{
                        //包含部分，则要加入未包含的部分
                        arr.forEach(i=>{
                            if(!this.form.rules.includes(i.id)) this.form.rules.push(i.id);
                        })
                    }
                }else {
                    //不包含，则加入所有
                    arr.forEach(i=>{this.form.rules.push(i.id);})
                }
            },
            checkAll(){
                const value = [];
                if(this.all){
                    this.all = false;
                }else{
                    this.listData.filter(auth=>{
                        auth.children.filter(a=>{
                            a.children.filter(i=>{
                                value.push(i.id);
                            })
                        })
                    });
                    this.all = true;
                }
                this.form.rules = value;
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if(!this.ruleForm.rules.length){
                            this.$message.error('请至少选择一个角色拥有的权限');return false;
                        }else{
                            this.buttonLoading = true;
                            this.buttonText = '提交中...';
                            this.formDisabled = true;
                            RoleAccessUpdate(this.ruleForm).then((response)=>{
                                this.buttonLoading = false;
                                this.buttonText = '提 交';
                                this.formDisabled = false;
                                if(response.code){
                                    this.$message.error(response.msg);
                                }else{
                                    this.$message({
                                        type: 'success',
                                        message: response.msg,
                                        duration: '1000',
                                        onClose:() =>{
                                            this.$router.push({name:'Role'});
                                        }
                                    });
                                }
                            }).catch(error=>{
                                this.buttonLoading = false;
                                this.buttonText = '提 交';
                                this.formDisabled = false;
                                this.$message.error(error.response.statusText);
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
        }
    }
</script>
